import Vue from 'vue'
import VueRouter from 'vue-router'
Vue.use(VueRouter)
// (1)防止重复使用 (导航重复避免了到当前的冗余导航)【就是减小报错】
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push (location) {
  return originalPush.call(this, location).catch(err => err)
}
const router = new VueRouter({
  routes: [
    // 登录页面
    { path: '/', redirect: '/home', meta: { title: '登录页面' } },

    {
      path: '/login',
      component: () => import('@/views/login/login'),
      meta: { title: '登录页面' }
    },
    // 登录校验页面,登录成功后 通过比较不同的hash值,确定是调用$router.back()方法还是调用$router.push('/home')方法
    {
      path: '/loginCheck',
      component: () => import('@/views/login/login'),
      meta: { title: '登录页面' }
    },
    // 我的信息
    {
      path: '/user',
      component: () => import('@/views/home/my/user/user.vue'),
      meta: { title: '我的信息' }
    },
    // 修改信息
    {
      path: '/panel',
      component: () => import('@/views/home/my/mode/panel.vue'),
      meta: { title: '修改信息' }
    },
    // 上传头像
    {
      path: '/uploadingfiles',
      component: () =>
        import('@/views/home/my/uploadingfiles/uploadingfiles.vue'),
      meta: { title: '上传头像' }
    },
    // 查看信息
    {
      path: '/information',
      component: () => import('@/views/home/my/user/information.vue'),
      meta: { title: '查看信息' }
    },
    // 在招职位
    {
      path: '/recruitment',
      component: () => import('@/views/home/company/com/recruitment.vue'),
      meta: { title: '在招职位' }
    },
    // (公司)企业详情
    {
      path: '/details',
      component: () => import('@/views/home/company/com/details.vue'),
      meta: { title: '企业详情' }
    },
    // (公司)企业刷题
    {
      path: '/questions',
      component: () => import('@/views/home/company/com/questions.vue'),
      meta: { title: '企业面试题' }
    },
    {
      // 主页面
      path: '/home',
      component: () => import('@/views/home/home'),
      redirect: '/home/compony',
      children: [
        {
          // 公司页面
          path: 'compony',
          component: () => import('@/views/home/company/company'),
          meta: {
            index: 1,
            title: '公司页面'
          }
        },
        {
          // 刷题页面
          path: 'subject',
          component: () => import('@/views/home/subject/subject'),
          meta: {
            index: 2,
            title: '刷题页面'
          }
        },
        {
          // 发现页面
          path: 'find',
          component: () => import('@/views/home/find/find'),
          meta: {
            index: 3,
            title: '发现页面'
          }
        },
        {
          // 我的页面
          path: 'my',
          component: () => import('@/views/home/my/my'),
          meta: {
            index: 4,
            title: '我的页面'
          }
        }
      ]
    },
    {
      path: '/company/',
      component: () => import('@/views/home/company/detail/detail.vue'),
      meta: {
        title: '公司详情'
      }
    },
    {
      // 发现页-面试技巧列表页
      path: '/interviewList',
      component: () => import('@/views/home/find/interview/interviewList.vue'),
      meta: {
        title: '面试技巧列表'
      }
    },
    {
      // 发现页-面试技巧搜索页
      path: '/interviewSearch',
      component: () =>
        import('@/views/home/find/interview/interviewSearch.vue'),
      meta: {
        title: '面试技巧搜索'
      }
    },
    {
      // 发现页-面试技巧详情页
      path: '/interviewDetail/:id',
      component: () =>
        import('@/views/home/find/interview/interviewDetail.vue'),
      meta: {
        title: '面试技巧详情'
      }
    },
    {
      // 发现页-数据图表
      path: '/chart',
      component: () => import('@/views/home/find/chart/chart.vue'),
      meta: {
        title: '市场数据'
      }
    },
    {
      // 发现页-面经列表页
      path: '/experienceList',
      component: () =>
        import('@/views/home/find/experience/experienceList.vue'),
      meta: {
        title: '面经列表'
      }
    },
    {
      // 发现页-面经搜索页
      path: '/experienceListSearch',
      component: () =>
        import('@/views/home/find/experience/experienceListSearch.vue'),
      meta: {
        title: '面经搜索'
      }
    },
    {
      // 发现页-面经详情页
      path: '/experienceDetail/:id',
      component: () =>
        import('@/views/home/find/experience/experienceDetail.vue'),
      meta: {
        title: '面经详情'
      }
    },
    {
      path: '/company/detail',
      component: () => import('@/views/home/company/detail/detail.vue')
    },
    {
      path: '/answerCard',
      component: () => import('@/views/home/subject/answerCard.vue'),
      meta: {
        title: '开始答题'
      }
    }
  ]
})
// 6. 路由导航守卫(前)
router.beforeEach((to, from, next) => {
  next()
  // console.log('去哪里:', to)
  // console.log('从哪来', from)
})
// 7.路由导航守卫(后)
router.afterEach(to => {
  // 7.1 修改标题的名字
  document.title = to.meta.title
})

export default router
